﻿using System;
using System.Configuration;
using System.IO;
using System.Threading;

namespace Project.Tool
{
	/// <summary>
	/// 写日志类
	/// </summary>
	public class Log
	{
		#region 写日志
		/// <summary>
		/// 写日志
		/// </summary>
		public static void AddLog(string log)
		{
			string 
				logPath = ConfigurationManager.AppSettings["LogPath"] + "\\Sever_Log.txt";

			LogConfig.WriteFile(log, logPath);
		}
		#endregion

		#region 写错误日志
		/// <summary>
		/// 写错误日志
		/// </summary>
		public static void AddErr(string log)
		{
			string logPath = ConfigurationManager.AppSettings["LogPath"] + "\\Sever_Error_Log.txt";

			LogConfig.WriteFile(log, logPath);
		}
		#endregion
	}

	public class LogConfig
	{
		#region 字段
		public static readonly object _lock = new object();
		#endregion

		#region 指定文件写日志

		/// <summary>
		/// 写文件
		/// </summary>
		public static void WriteFile(string log, string path)
		{
			path = path.ServerPath();

			string logDirPath = ConfigurationManager.AppSettings["LogPath"];

			if ( !Directory.Exists(logDirPath.ServerPath()) )
				Directory.CreateDirectory(logDirPath.ServerPath());

			#region 原 方法注释
				ThreadStart ts = new ThreadStart(() =>{
					try
					{
						#region 日志内容
						string value = string.Format(
									Environment.NewLine +
									"/**" +
									Environment.NewLine +
									" *  操作时间： {0}" +
									Environment.NewLine +
									" *  执行事件：{1}" +
									Environment.NewLine +
									" *  执行结果： {2}" +
									Environment.NewLine +
									" */",
									DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
									log.ToString(),
									"已完成");
						#endregion

						File.AppendAllText(path,value);
					}
					catch
					{

					}
				});

				Thread thread = new Thread(ts);

				thread.Start();
			#endregion
		}

		static void addLogStr()
		{

		}

		#endregion
	}
}
